📒 Notes for Lecture 21: CSS Specificity Deep Dive
In this lecture, we’ll learn how CSS specificity determines which rules win when multiple selectors target the same element. We’ll cover:
- The specificity ranking: inline > ID > class/attribute/pseudo-class > element/pseudo-element > universal.
- How equal-specificity rules resolve based on order of appearance.
- Using
!important
to override even inline styles. - Practical demos across six HTML files to see each case in action:
element.html
– Element vs. universal selector.classattribute.html
– Class selectors vs. attribute selector with equal specificity.idselector.html
– ID vs. class vs. element selectors.importance.html
–!important
overriding all specificity.inline.html
– Inline style overriding ID, class, and element.universal.html
– Universal selector vs. element selector.
Hinglish: Lecture 21 mein hum CSS specificity ko detail mein dekhenge. Yeh teach karega ke agar
ek element
par multiple selectors apply ho rahe hain, toh kaunsa rule baad mein apply hoga. Specificity
ranking yeh hogi:
inline styles sabse zyada (1000), uske baad ID selectors (100), phir
classes/attributes/pseudo-classes (10),
phir element/pseudo-elements (1), aur sabse kam universal selector (0). Agar do rules ki
specificity barabar ho,
toh jo rule CSS file mein last likha hoga, woh apply hoga. !important
use karke aap
in sab ko override
kar sakte ho. Hum chhe demo files ke through har case ko practically dekhenge.
1. element.html
- English Explanation:
In
element.html
, two rules target the sameh1
. The universal selector* { color: gray; }
applies gray to all elements, but the element selectorh1 { color: tomato; background-color: black; }
has higher specificity (0,0,0,1) than the universal selector (0,0,0,0). Therefore, headings render in tomato on black, overriding gray.
Hinglish: element.html
mein humne ek universal selector (*
) aur ek
element selector (h1
) dekha. Universal selector sab elements ko gray declare karta
hai (specificity 0,0,0,0), par element selector (h1
) ki specificity (0,0,0,1) zyada
hoti hai. Isliye h1
tomato on black dikhega, gray nahin.
💻 Live Preview: element.html
If the iframe doesn’t load, click here to open element.html in a new tab.
2. classattribute.html
- English Explanation:
In
classattribute.html
, three class selectors (.h1
,.cred
,.cpurple
) and one attribute selector ([data-x="a"]
) all have the same specificity (0,0,1,0). When specificity ties, the rule declared last wins. Here, the attribute selector is last in the stylesheet, so itscolor: aqua; background-color: red;
styling overrides the earlier class-based colors.
Hinglish: classattribute.html
mein teen class selectors (.h1
,
.cred
, .cpurple
) aur ek attribute selector ([data-x="a"]
)
ki specificity sab (0,0,1,0) hai. Jab specificity same ho, sabse last likha gaya rule apply hota
hai. Yahan attribute selector sabse last hai, isliye aqua/red styling class selectors ko
override karta hai.
💻 Live Preview: classattribute.html
If the iframe doesn’t load, click here to open classattribute.html in a new tab.
3. idselector.html
- English Explanation:
In
idselector.html
, several selectors compete:- Universal (
* { color: gray; }
, specificity 0,0,0,0) - Element (
h1 { color: pink; }
, specificity 0,0,0,1) - Class (
.h1 { color: blue; }
, specificity 0,0,1,0) - ID (
#title { color: red; }
, specificity 0,1,0,0)
- Universal (
Hinglish: idselector.html
mein multiple selectors hain: universal (*
),
element (h1
), class (.h1
), aur ID (#title
). ID selector
ki specificity (0,1,0,0) sabse zyada hoti hai, isliye ID rule red apply karta hai aur heading
red dikhta hai.
💻 Live Preview: idselector.html
If the iframe doesn’t load, click here to open idselector.html in a new tab.
4. importance.html
- English Explanation:
In
importance.html
, twop
rules both target paragraphs. The second block uses!important
on every property:
This gives it effective specificity of 10,000, overriding the earlier rulep { color: red !important; font-size: 25px !important; background-color: yellow !important; }
p { color: blue; }
. As a result, all paragraphs appear red on yellow, ignoring the first rule.
Hinglish: importance.html
mein do p
selectors hain, par dusre rule
mein har property pe !important
use hua hai. !important
ki specificity
(10,000) sab selectors se zyada hai, isliye pehla rule (color: blue
) override ho
jaata hai aur sare paragraphs red on yellow dikhenge.
💻 Live Preview: importance.html
If the iframe doesn’t load, click here to open importance.html in a new tab.
5. inline.html
- English Explanation:
In
inline.html
, several selectors compete:- Universal (
* { color: gray; }
, specificity 0,0,0,0) - ID (
#title { color: red; background-color: yellow; }
, specificity 0,1,0,0) - Class (
.h1 { color: blue; }
, specificity 0,0,1,0) - Element (
h1 { color: pink; }
, specificity 0,0,0,1)
h1
tag also has an inline style attribute:
Inline style has specificity (1,0,0,0), which outranks the ID selector (0,1,0,0). Therefore, the heading appears aqua on blueviolet.…
- Universal (
Hinglish: inline.html
mein universal, ID, class, aur element selectors hain, par
inline style ki specificity (1,0,0,0) sabse zyada hoti hai (ID se bhi zyada). Isliye inline
style (color: aqua; background-color: blueviolet;
) apply hua, heading aqua on
blueviolet dikhega.
💻 Live Preview: inline.html
If the iframe doesn’t load, click here to open inline.html in a new tab.
6. universal.html
- English Explanation:
In
universal.html
, two rules compete:- Universal selector:
* { color: gray; background-color: blue; }
(specificity 0,0,0,0) - Paragraph selector:
p { color: white; background-color: black; font-size: 25px; }
(specificity 0,0,0,1)
h1
has no specific rule, so it inherits the universal styling (gray on blue). - Universal selector:
Hinglish: universal.html
mein universal selector (*
) aur paragraph
selector (p
) hain. Paragraph selector ki specificity (0,0,0,1) universal (0,0,0,0)
se zyada hai, isliye paragraphs white on black ban gaye. h1
par koi specific rule
nahin hai, toh woh gray on blue dikhega (universal style).
💻 Live Preview: universal.html
If the iframe doesn’t load, click here to open universal.html in a new tab.
7. main.html
(Combined Demo)
- English Explanation:
In
main.html
, all six demos (element.html
,classattribute.html
,idselector.html
,importance.html
,inline.html
, anduniversal.html
) are embedded together in a single page for side-by-side comparison. Each section demonstrates the specificity rules in one viewport, making it easy to observe how different rules interact simultaneously.
Hinglish: main.html
mein humne sare demos ek saath combine kiye hain:
element.html
,
classattribute.html
, idselector.html
, importance.html
,
inline.html
, aur universal.html
. Is se ek hi page mein sabhi
specificity
cases ek saath nazar aate hain, jisse aap dekh sakte hain kaise alag-alag selectors ek-dusre ko
override
karte hain.
💻 Live Preview: main.html
If the iframe doesn’t load, click here to open main.html in a new tab.